﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;

namespace WebApp
{
    public partial class EditCargoFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditCargoFrm), this);
            var caseId = Request.Params["CaseId"];
            if (string.IsNullOrEmpty(caseId))
            {
                Response.Write("非法进入");
                Response.End();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Cargo> GetCargo(int id)
        {
            AjaxResult<TB_Cargo> result = new AjaxResult<TB_Cargo>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetCargo(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveCargo(string id, int caseId,int orderId, string cargoName, string amount, string hairWeight, string netWeight, string volume, string packageType, string caseAmount, string marker, string memo)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = !string.IsNullOrEmpty(id);
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                TB_User user = Session["User"] as TB_User;
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                TB_Cargo c = new TB_Cargo();                
                if (update)
                    c = this.BaseDataProxy.GetCargo(Convert.ToInt32(id));
                WebCommon.EvalControlValueToData(c, "amount", amount);
                WebCommon.EvalControlValueToData(c, "cargoName", cargoName);
                WebCommon.EvalControlValueToData(c, "case", caseId);
                WebCommon.EvalControlValueToData(c, "caseAmount", caseAmount);
                WebCommon.EvalControlValueToData(c, "hairWeight", hairWeight);
                WebCommon.EvalControlValueToData(c, "marker", marker);
                WebCommon.EvalControlValueToData(c, "memo", memo);
                WebCommon.EvalControlValueToData(c, "netWeight", netWeight);
                WebCommon.EvalControlValueToData(c, "packageType", packageType);
                WebCommon.EvalControlValueToData(c, "volume", volume);

                if (update)
                {
                    this.BaseDataProxy.UpdateCargo(c);
                    result.Data = c.id;
                }
                else
                {
                    int newId = this.BaseDataProxy.AddCargo(c);
                    result.Data = newId;
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteCargo(int id, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteCargo(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
